﻿Imports Mysoft.Map.Data
Imports System.Data
Imports System.Xml
Imports System.Text
Imports System.Data.SqlClient
Imports System.IO


Partial Public Class Cwjk_PzRuleSet_ErrorEntry
    Inherits Mysoft.Map.Application.Security.AppPage

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim strCwztGUID As String = Request.QueryString("CwztGUID")
            If Not strCwztGUID Is Nothing AndAlso strCwztGUID <> "" Then
                Me.txtCwztGUID.Text = strCwztGUID
                BindErrorPzEntryRule(strCwztGUID)
            End If

        End If
    End Sub

    Sub BindErrorPzEntryRule(ByVal strCwztGUID As String)
        Dim strBuild As StringBuilder = New StringBuilder()
        Dim dtGroupName, dtErrorPzRule As DataTable
        strBuild.Append(" SELECT DISTINCT a.OperTypeGUID,parent.OperType AS ParentOperType,b.OperType,a.GroupName, a.PzName,b.HierarchyCode,a.PzOrderCode ")
        strBuild.Append(" FROM p_CwjkPzEntryRule a INNER JOIN p_CwjkOperType b ON a.OperTypeGUID=b.OperTypeGUID ")
        strBuild.Append(" LEFT JOIN p_CwjkOperType parent ON b.CwztGUID=parent.CwztGUID AND b.HierarchyCode LIKE parent.HierarchyCode + '.%' ")
        strBuild.Append(" WHERE a.IsDisable=0 AND b.IsDisable=0 AND KjkmGUID Not IN (select KjkmGUID from p_CwjkKjkm where (IsEnd=1 AND ObjectType=0) or (ObjectType=1)) ")
        strBuild.Append(" AND a.CwztGUID= '" & strCwztGUID & "' ORDER BY b.HierarchyCode,a.PzOrderCode ; ")

        strBuild.Append(" SELECT  OperTypeGUID,PzEntryRuleGUID,GroupName,PzName,EntryNum,YDDirection,KjkmGUID,KjkmOrderCode,KjkmFullName,JyKjkmGUID,JyKjkmFullName,IsYC ")
        strBuild.Append(" FROM  vp_CwjkPzEntryRuleYC WHERE CwztGUID= '" & strCwztGUID & "' ORDER BY HierarchyCode,PzOrderCode,EntryNum ;")

        Dim conn As SqlConnection = New SqlConnection(MyDB.GetSqlConnectionString)
        Dim cmd As SqlCommand = New SqlCommand()
        Dim adapter As SqlDataAdapter = New SqlDataAdapter()
        Dim ds As DataSet = New DataSet()
        cmd.Connection = conn
        cmd.CommandText = strBuild.ToString()
        adapter.SelectCommand = cmd
        Try
            conn.Open()
            adapter.Fill(ds)
            adapter.Dispose()
            conn.Close()
            conn.Dispose()
        Catch ex As Exception
            If conn.State = ConnectionState.Open Then
                conn.Close()
                conn.Dispose()
                Throw New Exception("系统运行异常,请与系统管理员联系！")
            End If
        End Try
        If ds.Tables.Count <> 2 Then
            Return
        End If
        dtGroupName = ds.Tables(0)
        dtErrorPzRule = ds.Tables(1)

        If dtGroupName.Rows.Count <= 0 Then
            Return
        End If

        Dim xmlDoc As XmlDocument = New XmlDocument()
        Dim groupNameEl, pzEntryEl As XmlElement
        Dim i, j As Integer
        Dim strParentOperType, strOperType, strOperTypeGUID, strGroupName, strPzName, strPzOrderCode, strFullGroupName As String
        xmlDoc.LoadXml("<OperType CwztGUID=""" & strCwztGUID & """></OperType>")
        For i = 0 To dtGroupName.Rows.Count - 1
            strFullGroupName = ""
            groupNameEl = xmlDoc.CreateElement("GN")
            strOperTypeGUID = dtGroupName.Rows(i).Item("OperTypeGUID").ToString()
            strOperType = dtGroupName.Rows(i).Item("OperType").ToString()
            strParentOperType = dtGroupName.Rows(i).Item("ParentOperType").ToString()
            strGroupName = dtGroupName.Rows(i).Item("GroupName").ToString()
            strPzName = dtGroupName.Rows(i).Item("PzName").ToString()
            strPzOrderCode = dtGroupName.Rows(i).Item("PzOrderCode").ToString()

            strFullGroupName = strParentOperType & " -> " & strOperType
            If strGroupName <> "" Then
                strFullGroupName = strFullGroupName & " -> " & strGroupName
                If strPzName <> "" Then
                    strFullGroupName = strFullGroupName & " -> " & strPzName
                End If
            Else
                If strPzName <> "" Then
                    strFullGroupName = strFullGroupName & " -> " & strPzName
                End If
            End If

            groupNameEl.SetAttribute("FullGroupName", strFullGroupName)
            groupNameEl.SetAttribute("OperTypeGUID", strOperTypeGUID)
            groupNameEl.SetAttribute("PzOrderCode", strPzOrderCode)
            Dim bIsHasJyKm As Boolean = True
            For j = 0 To dtErrorPzRule.Rows.Count - 1
                If strOperTypeGUID = dtErrorPzRule.Rows(j).Item("OperTypeGUID").ToString() AndAlso strGroupName = dtErrorPzRule.Rows(j).Item("GroupName").ToString() AndAlso strPzName = dtErrorPzRule.Rows(j).Item("PzName").ToString() Then
                    pzEntryEl = xmlDoc.CreateElement("ErrorEntry")
                    Dim strKjkmGUID As String = dtErrorPzRule.Rows(j).Item("KjkmGUID").ToString()
                    Dim strKjkmOrderCode As String = dtErrorPzRule.Rows(j).Item("KjkmOrderCode").ToString()
                    Dim strJyKjkmGUID, strJyKjkmFullName As String
                    strJyKjkmGUID = ""
                    strJyKjkmFullName = ""
                    '查找建议会计科目
                    If dtErrorPzRule.Rows(j).Item("IsYC").ToString() = "1" AndAlso Not GetJyKjkm(strKjkmOrderCode, strCwztGUID, strJyKjkmGUID, strJyKjkmFullName) Then
                        bIsHasJyKm = False
                    End If
                    pzEntryEl.SetAttribute("PzEntryRuleGUID", dtErrorPzRule.Rows(j).Item("PzEntryRuleGUID").ToString())
                    pzEntryEl.SetAttribute("EntryNum", dtErrorPzRule.Rows(j).Item("EntryNum").ToString())
                    pzEntryEl.SetAttribute("YDDirection", dtErrorPzRule.Rows(j).Item("YDDirection").ToString())
                    pzEntryEl.SetAttribute("KjkmGUID", strKjkmGUID)
                    pzEntryEl.SetAttribute("KjkmFullName", dtErrorPzRule.Rows(j).Item("KjkmFullName").ToString())
                    pzEntryEl.SetAttribute("JyKjkmGUID", strJyKjkmGUID)
                    pzEntryEl.SetAttribute("JyKjkmFullName", strJyKjkmFullName)
                    pzEntryEl.SetAttribute("IsYC", dtErrorPzRule.Rows(j).Item("IsYC").ToString())

                    groupNameEl.AppendChild(pzEntryEl)
                End If
            Next
            If bIsHasJyKm Then
                groupNameEl.SetAttribute("IsHasJyKm", "1")
            Else
                groupNameEl.SetAttribute("IsHasJyKm", "0")
            End If
            xmlDoc.DocumentElement.AppendChild(groupNameEl)
        Next
        If xmlDoc.HasChildNodes Then
            tr_ErrorTip.Style("display") = ""
            tr_SaveError.Style("display") = ""
            div_ErrorEntry.InnerHtml = TransXSLT(xmlDoc, "/PubProject/Cwjk/Cwjk_PzRuleSet_ErrorEntry.xslt")
        End If

    End Sub

    '得到建议会计科目
    Function GetJyKjkm(ByVal strKjkmOrderCode As String, ByVal strCwztGUID As String, ByRef strJyKjkmGUID As String, ByRef strJyKjkmFullName As String) As Boolean
        If strCwztGUID = "" Then
            Return False
        End If
        Dim strSQL As String = ""
        Dim dtSonKmTable, dtParentKmTable As DataTable

        '查找子级科目
        strSQL = " SELECT KjkmGUID,KjkmFullName FROM p_CwjkKjkm WHERE ((IsEnd=1 AND ObjectType=0) or (ObjectType=1)) AND CwztGUID = '" & strCwztGUID & "' AND KjkmOrderCode like '" & strKjkmOrderCode & ".%'"
        dtSonKmTable = MyDB.GetDataTable(strSQL)

        strSQL = " SELECT KjkmGUID,KjkmFullName FROM p_CwjkKjkm WHERE ((IsEnd=1 AND ObjectType=0) or (ObjectType=1)) AND CwztGUID = '" & strCwztGUID & "' AND  '" & strKjkmOrderCode & "' like KjkmOrderCode + '.%'"
        dtParentKmTable = MyDB.GetDataTable(strSQL)

        If dtParentKmTable.Rows.Count = 0 And dtSonKmTable.Rows.Count = 1 Then
            strJyKjkmGUID = dtSonKmTable.Rows(0).Item("KjkmGUID").ToString()
            strJyKjkmFullName = dtSonKmTable.Rows(0).Item("KjkmFullName").ToString()
            Return True
        End If
        If dtSonKmTable.Rows.Count = 0 And dtParentKmTable.Rows.Count = 1 Then
            strJyKjkmGUID = dtParentKmTable.Rows(0).Item("KjkmGUID").ToString()
            strJyKjkmFullName = dtParentKmTable.Rows(0).Item("KjkmFullName").ToString()
            Return True
        End If
        Return False
    End Function


    Public Shared Function TransXSLT(ByRef xmlDoc As XmlDocument, ByVal xsltUrl As String) As String
        Try
            Dim xslt As Xsl.XslCompiledTransform = GetXsltDoc(xsltUrl)
            Dim output As New StringBuilder("")
            Dim writer As New StringWriter(output)
            xslt.Transform(xmlDoc, Nothing, writer)
            Return output.ToString()
        Catch ex As Exception
            Throw New Exception("系统运行异常,请与系统管理员联系！")
        End Try

    End Function

    ''' <summary>
    ''' 从缓存中获取指定URL的XSLT文件文档对象
    ''' </summary>
    ''' <param name="url">XSLT文件路径</param>
    ''' <returns>XslCompiledTransform</returns>
    ''' <remarks></remarks>
    Public Shared Function GetXsltDoc(ByVal url As String) As Xsl.XslCompiledTransform
        Try
            Dim key As String = url

            Dim xslt As Xsl.XslCompiledTransform

            xslt = Mysoft.Map.Caching.MyCache.Get(key)

            If xslt Is Nothing Then
                xslt = New Xsl.XslCompiledTransform
                Dim path As String = HttpContext.Current.Server.MapPath(url)
                xslt.Load(path)
                Mysoft.Map.Caching.MyCache.Insert(key, xslt, New System.Web.Caching.CacheDependency(path))
            End If
            Return xslt
        Catch ex As Exception
            Return Nothing
        End Try
    End Function
End Class